var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    show_info:{},
    car_list: [],
    car_type: [],
    tab_list: [
      { title: '首页', icon: 'fangzi' },
      { title: '车型报价', icon: 'chuzuche' },
      { title: '门店简介', icon: 'log' }
    ],
    tab_index: 0,
    is_display: false,
    sort_display: false,
    search_title: '全部品牌▼',
    sort_title: '默认排序▼',
    carBrandId: 0,
    sortNum: 0
  },
  jumpmini:function(){
    wx.navigateToMiniProgram({
      appId: 'wx91169743893f29bf',
      path: 'pages/index/index',
      envVersion: 'release',
      success(res) {
        // 打开成功
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var t = this
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function () {
      t.getlist();
    }, 200)
  },
  getlist:function(options){
    var appid = app.globalData.appid
    var options = { "appid": appid }
    var option = { "shop_id": app.globalData.shop_id };
    console.log(app.globalData)
    let that = this;
    wx.showLoading({
      title: '加载中…',
    })
    //ver-jionEndTime
    app.ihttpGet('https://www.58zltc.com/api.php/Car/verJionEndTime/appid/' + options.appid, function (res) {
      that.setData({
        verJoinEndTime: res.data
      })
    })
    //所有车型
    app.ihttpGet('https://www.58zltc.com/api.php/Car/getAllCarBrand/appid/' + options.appid, function (res) {
      that.setData({
        car_type: res.data
      })
    })
    app.ihttpGet('https://www.58zltc.com/api.php/Car/getTypeCar/typeid/0/shop_id/' + option.shop_id, function (res) {
      //console.log(res.data);
      // wx.showModal({
      //   title: 'a',
      //   content: res.data,
      // })
      for (var index in res.data) {
        var str = res.data[index].title;
        var strArr = str.split(' ');
        res.data[index].titleAll = strArr[1] + '\n' + strArr[0] + '\t' + strArr[2] + '\t' + strArr[3];
        res.data[index].title = res.data[index].title.substring(0, 15) + '...';
      }
      that.setData({
        car_list: res.data
      })
    })
    app.ihttpGet('https://www.58zltc.com/api.php/Car/shop/shop_id/' + option.shop_id, function (res) {
      var show_info;
      if (options.member_id) {
        res.data.member.forEach(function (value, index) {

          if (value.id == options.member_id) {
            show_info = { logo_url: value.headimg, name: value.name + '的网上车店', tel: value.tel }
          }
        })
      } else {
        show_info = { logo_url: res.data.logo_url, name: res.data.name, tel: res.data.tel }
      }
      that.setData({
        info: res.data,
        show_info: show_info
      })
      wx.setStorage({
        key: 'shop_show_info',
        data: show_info
      })
      wx.setNavigationBarTitle({
        title: show_info.name,
      })
      setTimeout(function () {
        wx.hideLoading();
      }, 3000)
    })  
  },
  setTab: function(e){
    this.setData({
      tab_index: e.currentTarget.dataset.index
    })
  },
  call: function () {
    const tel = this.data.show_info.tel;
    wx.makePhoneCall({
      phoneNumber: tel,
    })
  },
  openLocation: function(){
    const data = this.data.info;
    wx.openLocation({
      latitude: Number(data.latitude),
      longitude: Number(data.longitude),
      name: data.name,
      address: data.address
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const shop = this.data.info;
    return {
      title: shop.name,
      path: '/pages/car/detail?shop_id=' + shop.shop_id
    }
  },

  clickMenuAll: function () {
    var that = this;
    if (this.data.is_display) {
      this.setData({
        search_title: that.data.search_title.replace('▲', '▼'), 
        is_display: false
      })
    } else {
      this.setData({
        search_title: that.data.search_title.replace('▼', '▲'), 
        is_display: true,
        sort_display: false
      })
    }
  },

  clickCarType: function (e) {
    this.setData({
      carBrandId: e.currentTarget.dataset.id
    })
    var option = { "shop_id": app.globalData.shop_id };
    var id = this.data.carBrandId;
    let that = this;
    //分类车型
    app.ihttpGet('https://www.58zltc.com/api.php/Car/getTypeCar/typeid/' + id + '/shop_id/' + option.shop_id, function (res) {
      for (var index in res.data) {
        var str = res.data[index].title;
        var strArr = str.split(' ');
        res.data[index].titleAll = strArr[1] + '\n' + strArr[0] + '\t' + strArr[2] + '\t' + strArr[3];
        res.data[index].title = res.data[index].title.substring(0, 15) + '...';
      }
      that.setData({
        car_list: res.data
      })
    })
    this.setData({
      search_title: e.currentTarget.dataset.name + '▼',
      sort_title: '默认排序▼',
      is_display: false
    })
  },

  clickCarTypeBrand: function (e) {
    this.setData({
      carBrandId: e.currentTarget.dataset.id
    })
    var option = { "shop_id": app.globalData.shop_id };
    var id = this.data.carBrandId;
    let that = this;
    //分类车型
    app.ihttpGet('https://www.58zltc.com/api.php/Car/getTypeCar/typeid/' + id + '/shop_id/' + option.shop_id, function (res) {
      for (var index in res.data) {
        var str = res.data[index].title;
        var strArr = str.split(' ');
        res.data[index].titleAll = strArr[1] + '\n' + strArr[0] + '\t' + strArr[2] + '\t' + strArr[3];
        res.data[index].title = res.data[index].title.substring(0, 15) + '...';
      }
      that.setData({
        car_list: res.data
      })
    })
    this.setData({
      tab_index: 1,
      search_title: e.currentTarget.dataset.name + '▼',
      sort_title: '默认排序▼',
      is_display: false
    })
  },

  clickMenuSort: function () {
    var that = this;
    if (this.data.sort_display) {
      this.setData({
        sort_title: that.data.sort_title.replace('▲', '▼'),
        sort_display: false
      })
    } else {
      this.setData({
        sort_title: that.data.sort_title.replace('▼', '▲'),
        sort_display: true,
        is_display: false
      })
    }
  },

  clickCarSort: function (e) {
    this.setData({
      sortNum: e.currentTarget.dataset.num
    })
    var sort_num = this.data.sortNum;
    let that = this;
    //分类车型
    app.ihttpGet('https://www.58zltc.com/api.php/Car/getSortCar/num/' + sort_num + '/typeid/' + that.data.carBrandId, function (res) {
      for (var index in res.data) {
        var str = res.data[index].title;
        var strArr = str.split(' ');
        res.data[index].titleAll = strArr[1] + '\n' + strArr[0] + '\t' + strArr[2] + '\t' + strArr[3];
        res.data[index].title = res.data[index].title.substring(0, 15) + '...';
      }
      that.setData({
        car_list: res.data
      })
    })
    this.setData({
      sort_title: e.currentTarget.dataset.name + '▼',
      sort_display: false
    })
  }
})